﻿Imports WPFSearchDataGrid.EventAggregator

Namespace Assets

    Public Class DataItemFactory

        Shared Function Resolve(column As DataGridColumn, publisher As IEventAggregator) As IDataItem
            Try
                Dim col = GetType(WPFSearchDataGrid).Assembly.GetExportedTypes() _
                        .Where(Function(t) Not t.IsInterface) _
                        .Where(Function(t) GetType(IDataItem).IsAssignableFrom(t)) _
                        .Where(Function(t) t.GetConstructors().Single().GetParameters().First().ParameterType Is column.GetType()).ToArray()
                If col.Count() = 1 Then
                    Return Activator.CreateInstance(col.First(), New Object() {column, publisher})
                Else
                    Return Activator.CreateInstance(GetType(TextBoxDataItem), New Object() {column, publisher})
                End If
            Catch ex As Exception
                MsgBox(String.Format("Unable to resolve an {0}; Are you missing an assembly?", column.ToString()), MsgBoxStyle.OkOnly, "Unable To Resolve Column Type")
                Return Nothing
            End Try
        End Function
    End Class

End Namespace